<?php
/**
 * Description of OpenQSCodeClass
 *
 * Open Question Single Code
 *
 * @author Chisheng Lu, Jeffy Shih
 * @copyright Copyright(c)2010 DoRISE team, Science Education center, NTNU.
 * @since 2010-03-31
 * @version 1.0
 */
class OpenQSCode {
    //put your code here
    private $mysqli;

    function __construct() {
        //引入資料庫連結參數設定
        include '../inc/oaConn.inc.php';
        $this->mysqli = @new mysqli($mysqlHost,$mysqlUser,$mysqlPassword,$mysqlDB);
        if(mysqli_connect_errno()) {
            throw new Exception('無法建立資料庫連線！'.mysqli_connect_error());
            $this->mysqli = false;
            exit();
        }

        //連線使用編碼
        $this->mysqli->query("SET NAMES 'utf8'");
    }

    // $aryOQSDim: Array
    // Returns Boolean
    public function addOpenQSCode($aryOQSC) {
        $intRtn = 0;

        $strNow = $this->getNowTime();
        $sql_addOQSC = 'INSERT INTO `OpenQSCode` (';
        $sql_values = ' VALUES(';
        if (array_key_exists('SQId', $aryOQSC)) {
            $sql_addOQSC .= '`SQId`, ';
            $sql_values .= $aryOQSC['SQId'] . ', ';
        }
        if (array_key_exists('OQSCategory', $aryOQSC)) {
            $sql_addOQSC .= '`OQSCategory`, ';
            $sql_values .= '\'' . $aryOQSC['OQSCategory'] . '\', ';
        }
        if (array_key_exists('OQSDef', $aryOQSC)) {
            $sql_addOQSC .= '`OQSDef`, ';
            $sql_values .= '\'' . $aryOQSC['OQSDef'] . '\', ';
        }
        if (array_key_exists('OQSExample', $aryOQSC)) {
            $sql_addOQSC .= '`OQSExample`, ';
            $sql_values .= '\'' . $aryOQSC['OQSExample'] . '\', ';
        }
        if (array_key_exists('UserId', $aryOQSC)) {
            $sql_addOQSC .= '`UserId`, ';
            $sql_values .= $aryOQSC['UserId'] . ', ';
        }
        $sql_addOQSC .= '`OQSCreated`)';
        $sql_values .= '\'' . $strNow . '\')';

        $sql_addOQSC .= $sql_values;
        $rs_addOQSC = $this->mysqli->query($sql_addOQSC);
        if (!$rs_addOQSC) {
            throw new Exception('新增問答題單編碼資料時發生錯誤！');
        }else {
            $intRtn = $this->mysqli->insert_id;
        }
        return $intRtn;

    }

    // $intOQSId: int
    // $aryOQSC: Array
    // Returns Boolean
    public function editOpenQSCode($intOQSId, $aryOQSC) {
        //檢查問答題單編碼編號是否為空白
        if (empty ($intOQSId)) {
            throw new Exception('缺少問答題單編碼編號！');
        }

        $bolRtn = false;
        $strNow = $this->getNowTime();
        $sql_editOQSC = 'UPDATE `OpenQSCode` SET ';
        $bolFirst = true;
        if (array_key_exists('SQId', $aryOQSC)) {
            $sql_editOQSC .= '`SQId` = ' . $aryOQSC['SQId'] . ', ';
        }
        if (array_key_exists('OQSCategory', $aryOQSC)) {
            $sql_editOQSC .= '`OQSCategory` = \'' . $aryOQSC['OQSCategory'] . '\', ';
        }
        if (array_key_exists('OQSDef', $aryOQSC)) {
            $sql_editOQSC .= '`OQSDef` = \'' . $aryOQSC['OQSDef'] . '\', ';
        }
        if (array_key_exists('OQSExample', $aryOQSC)) {
            $sql_editOQSC .= '`OQSExample` = \'' . $aryOQSC['OQSExample'] . '\', ';
        }
        if (array_key_exists('UserId', $aryOQSC)) {
            $sql_editOQSC .= '`OQSModifiedUserId` = ' . $aryOQSC['UserId'] . ', ';
        }
        $sql_editOQSC .= ' `OQSModified` = \'' . $strNow . '\' ';
        $sql_editOQSC .= 'WHERE `OQSId` = ' . $intOQSId;
        
        $rs_editOQSC = $this->mysqli->query($sql_editOQSC);
        if (!$rs_editOQSC) {
            throw new Exception('更新解釋範例資料時發生錯誤！');
        }else {
            $bolRtn = true;
        }
        return $bolRtn;
    }

    // $intOQSId: int
    // Returns Boolean
    public function delOpenQSCodeByOQSId($intOQSId) {
        $bolRtn = false;
        //檢查問答題單編碼編號是否為空白
        if (empty ($intOQSId)) {
            throw new Exception('缺少問答題單編碼編號。');
        }

        //刪除
        $sql_delOQSC = 'DELETE FROM `OpenQSCode` WHERE `OQSId`=' . $intOQSId;
        $rs_delOQSC = $this->mysqli->query($sql_delOQSC);
        if (!$rs_delOQSC) {
            throw new Exception('依問答單編碼編號，刪除問答題單編碼範例時發生錯誤。');
        }else {
            $bolRtn = true;
        }

        return $bolRtn;

    }

    public function delOpenQSCodeBySQId($intSQId) {
        $bolRtn = false;
        //檢查問答題單編碼編號是否為空白
        if (empty ($intSQId)) {
            throw new Exception('缺少子題編號，無法刪除問答題單編碼範例資料。');
        }

        //刪除解釋範例
        $sql_delOQSC = 'DELETE FROM `OpenQSCode` WHERE `SQId`=' . $intSQId;
        $rs_delOQSC = $this->mysqli->query($sql_delOQSC);
        if (!$rs_delOQSC) {
            throw new Exception('依子題編號，刪除問答題單編碼範例時發生錯誤。');
        }else {
            $bolRtn = true;
        }

        return $bolRtn;

    }

    // $intOQSId: int
    // Returns Array
    public function getOpenQSCodeInfoByOQSId($intOQSId) {
        if (empty($intOQSId)) {
            throw new Exception('缺少問答題單編碼編號，無法查詢問答題單編碼資料！');
        }

        $sql_getOQSC = 'SELECT * FROM `OpenQSCode` WHERE `OQSId`=' . $intOQSId;
        $rs_getOQSC = $this->mysqli->query($sql_getOQSC);
        if(!$rs_getOQSC) {
            throw new Exception('查詢問答題單編碼資料時發生錯誤！');
        }else {
            if ($rs_getOQSC->num_rows == 0) {
                throw new Exception('無此問答題單編碼編號的資料！');
            }else {
                $aryOQSC = $rs_getOQSC->fetch_row();
                $rs_getOQSC->free_result();
                return $aryOQSC;
            }
        }
    }

    // Returns Array
    public function getOpenQSCodeListBySQId($intSQId) {
        $sql_getOQSCList = 'SELECT * FROM `OpenQSCode` WHERE `SQId`=' . $intSQId . ' ORDER BY `OQSId`';
        $rs_getOQSCList = $this->mysqli->query($sql_getOQSCList);

        if (!$rs_getOQSCList) {
            throw new Exception('無法查詢該問答題單編碼的資料！');
        }else {
            $aryOQSCList = array();
            while ($aryOneOQSC = $rs_getOQSCList->fetch_row()) {
                array_push($aryOQSCList, $aryOneOQSC);
            }

            $rs_getOQSCList->free_result();

            return $aryOQSCList;
        } //End If

    }

    private function getNowTime() {
        return date('Y-m-d H:i:s');
    }

    function  __destruct() {
        if ($this->mysqli) {
            $this->mysqli->close();
            $this->mysqli = FALSE;
        }
    }
}
?>
